"""
    Stuff for sending messages.
"""

from fastapi import BackgroundTasks

# Libraries.
from fastapi_mail import MessageSchema

from afast_core.core_tools.singleton_helper import Singleton
from .config import _build_fastmail

# Core.



@Singleton
class FastMailEngine:
    pass
    def __init__(self, settings):
        self.fastmail = _build_fastmail(settings=settings())

    @classmethod
    async def _send_email(cls, email: str, subject: str, body: str):
        """执行发送邮件信息"""
        if not cls.fastmail:
            return  # Mail disabled.
        await cls.fastmail.send_message(MessageSchema(subject=subject, recipients=[email], body=body, subtype="plain"))

    async def send_verification_email(cls, background_tasks: BackgroundTasks, email: str, mention: str,
                                      confirmation_link: str):
        """发送登入验证码给用户"""
        subject = "登入XXXX!"
        message = f"你好, {mention}! 请点击下面的地址进行确认: {confirmation_link}"
        background_tasks.add_task(cls._send_email, email, subject, message)

    async def send_verification_end_email(cls, background_tasks: BackgroundTasks, email: str, mention: str):
        """Send verification end email to the user."""
        subject = "登入XXXX成功!"
        message = f"你好, {mention}! 账号创建成功！"
        background_tasks.add_task(cls._send_email, email, subject, message)

    async def send_code_email(cls, background_tasks: BackgroundTasks, email: str, mention: str, code: str):
        """Send 2FA one time password email to the user."""
        subject = "登入验证码确认!"
        message = f"你好, {mention}! 你的登入验证码为: {code}"
        background_tasks.add_task(cls._send_email, email, subject, message)
